package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Package;
import com.ultrasoft.farmasoft.domain.Pharmacyproduct;
import com.ultrasoft.farmasoft.domain.Userprovider;


@Repository(value = "packageDao")
@Transactional(readOnly = false)
public class PackageDaoImpl implements PackageDao {
	
	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	public List<Package> getListPackage() {
		TypedQuery<Package> q = em.createNamedQuery("Package.findAll",Package.class);
    	return q.getResultList();
	}

	public List<Package> getListPackageByActive(boolean active) {
		TypedQuery<Package> q = em.createNamedQuery("Package.findByActive",Package.class);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public List<Package> getListPackageByPharmacyProductByActive(Pharmacyproduct pharmacyproduct,boolean active) {
		TypedQuery<Package> q = em.createNamedQuery("Package.findByProviderIdByProductIdByPharmacyIdByActive",Package.class);
    	q.setParameter("providerId",pharmacyproduct.getPharmacyproductPK().getProviderId());
    	q.setParameter("productId",pharmacyproduct.getPharmacyproductPK().getProductId());
    	q.setParameter("pharmacyId",pharmacyproduct.getPharmacyproductPK().getPharmacyId());
    	q.setParameter("active",active);
    	return q.getResultList();
	}
	
	public List<Package> getListPackageByPharmacyProductByActiveByState(Pharmacyproduct pharmacyproduct,boolean active,short state) {
		TypedQuery<Package> q = em.createNamedQuery("Package.findByProviderIdByProductIdByPharmacyIdByActiveByState",Package.class);
    	q.setParameter("providerId",pharmacyproduct.getPharmacyproductPK().getProviderId());
    	q.setParameter("productId",pharmacyproduct.getPharmacyproductPK().getProductId());
    	q.setParameter("pharmacyId",pharmacyproduct.getPharmacyproductPK().getPharmacyId());
    	q.setParameter("state",state);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public List<Package> getListPackageByUserProvider(Userprovider userprovider,boolean active) {
		TypedQuery<Package> q = em.createNamedQuery("Package.findByProviderIdByUserProviderIdByActive",Package.class);
    	q.setParameter("providerId",userprovider.getUserproviderPK().getProviderId());
    	q.setParameter("userProviderId",userprovider.getUserproviderPK().getUserProviderId());
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public Package findPackageByPackageId(Integer packageId) {
		Package res=null;
		try {
			res=em.find(Package.class,packageId);
		} catch (Exception e) {
			// TODO: handle exception
		}
    	return res;
	}

	public void update(Package packageElement) {
		em.merge(packageElement);
	}

	public void remove(Package packageElement) {
		em.remove(em.getReference(Package.class,packageElement.getPackageId()));
	}

	public void add(Package packageElement) {
		em.persist(packageElement);
	}
}
